package com.hmall.service;

import com.hdmall.business.common.common.ProductPrice;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallAttrConnectMapper;
import com.hmall.mapper.MallAttrValueMapper;
import com.hmall.mapper.MallPriceMapper;
import com.hmall.mapper.MallProductMapper;
import com.hmall.model.MallAttrConnect;
import com.hmall.model.MallAttrValue;
import com.hmall.model.MallPrice;
import com.hmall.model.MallProduct;
import com.hmall.vo.BProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MallAttrConnectService {
    private final MallAttrConnectMapper mallAttrConnectMapper;

    private final MallAttrValueMapper mallAttrValueMapper;

    private final MallProductMapper mallProductMapper;

    private final
    MallPriceMapper mallPriceMapper;

    @Autowired
    public MallAttrConnectService(MallAttrConnectMapper mallAttrConnectMapper, MallAttrValueMapper mallAttrValueMapper, MallProductMapper mallProductMapper, MallPriceMapper mallPriceMapper) {
        this.mallAttrConnectMapper = mallAttrConnectMapper;
        this.mallAttrValueMapper = mallAttrValueMapper;
        this.mallProductMapper = mallProductMapper;
        this.mallPriceMapper = mallPriceMapper;
    }

    public void saveAttrConnect(MallAttrConnect connect){
        mallAttrConnectMapper.insert(connect);
    }

    public void removeConnect(Integer product_id){
        mallAttrConnectMapper.removeProduct(product_id);
    }

    public Integer checkCategory(String category){
        return mallAttrValueMapper.checkCategory(category);
    }

    public Integer checkFineness(String fineness){
        return mallAttrValueMapper.checkFineness(fineness);
    }
    public Integer checkGloss(String gloss){
        return mallAttrValueMapper.checkGloss(gloss);
    }
    public Integer checkSection(String section){
        return mallAttrValueMapper.checkSection(section);
    }
    public Integer checkPore_numberId(String pore_numberId){
        return mallAttrValueMapper.checkPore_numberId(pore_numberId);
    }
    public Integer checkSingle_shareId(String single_shareId){
        return mallAttrValueMapper.checkSingle_shareId(single_shareId);
    }

    public Integer checkModel(String model){
        return mallAttrValueMapper.checkModel(model);
    }

    public Integer checkColor(String color){
        return mallAttrValueMapper.checkColor(color);
    }

    public Integer checkGrade(String grade){
        return mallAttrValueMapper.checkGrade(grade);
    }

    public Integer myInsert(MallAttrValue mallAttrValue){
        return mallAttrValueMapper.myInsert(mallAttrValue);
    }
    public Integer findId(MallAttrValue attrValue){
        return mallAttrValueMapper.findId(attrValue);
    }

    public Integer findProductIdByModel(String model){
        return mallAttrConnectMapper.findProductIdByModel(model);
    }

    public ServerResponse findByProductId(Integer id){
        List attrlist=mallAttrConnectMapper.findByProductId(id);
        System.out.println(attrlist);
        System.out.println(attrlist.size());
        BProductVo productVo=new BProductVo();
        if (attrlist.size()==7){
            MallAttrConnect connect = (MallAttrConnect) attrlist.get(0);
            System.out.println(connect.toString());
            productVo.setCategory(connect.getAttrValue());
            MallAttrConnect connect2 = (MallAttrConnect) attrlist.get(1);
            productVo.setFineness(connect2.getAttrValue());
            MallAttrConnect connect3 = (MallAttrConnect) attrlist.get(2);
            productVo.setGloss(connect3.getAttrValue());
            MallAttrConnect connect4 = (MallAttrConnect) attrlist.get(3);
            productVo.setSection(connect4.getAttrValue());
            MallAttrConnect connect5 = (MallAttrConnect) attrlist.get(4);
            productVo.setPore_number(connect5.getAttrValue());
            MallAttrConnect connect6 = (MallAttrConnect) attrlist.get(5);
            productVo.setModel(connect6.getAttrValue());
            MallAttrConnect connect7 = (MallAttrConnect) attrlist.get(6);
            productVo.setColor(connect7.getAttrValue());
            productVo.setNetwork("");
            productVo.setTwisting("");
            productVo.setSingle_share("");
        }else {
            MallAttrConnect connect = (MallAttrConnect) attrlist.get(0);
            System.out.println(connect.toString());
            productVo.setCategory(connect.getAttrValue());
            MallAttrConnect connect2 = (MallAttrConnect) attrlist.get(1);
            productVo.setFineness(connect2.getAttrValue());
            MallAttrConnect connect3 = (MallAttrConnect) attrlist.get(2);
            productVo.setGloss(connect3.getAttrValue());
            MallAttrConnect connect4 = (MallAttrConnect) attrlist.get(3);
            productVo.setSection(connect4.getAttrValue());
            MallAttrConnect connect5 = (MallAttrConnect) attrlist.get(4);
            productVo.setPore_number(connect5.getAttrValue());
            MallAttrConnect connect6 = (MallAttrConnect) attrlist.get(5);
            productVo.setSingle_share(connect6.getAttrValue());
            MallAttrConnect connect7 = (MallAttrConnect) attrlist.get(6);
            productVo.setModel(connect7.getAttrValue());
            MallAttrConnect connect8 = (MallAttrConnect) attrlist.get(7);
            productVo.setColor(connect8.getAttrValue());
            MallAttrConnect connect9 = (MallAttrConnect) attrlist.get(8);
            productVo.setNetwork(connect9.getAttrValue());
            MallAttrConnect connect10 = (MallAttrConnect) attrlist.get(9);
            productVo.setTwisting(connect10.getAttrValue());
        }
        List<MallPrice> priceList=mallPriceMapper.findPriceList(id);
        List<ProductPrice> priceList1= new ArrayList<>();
//        for (int i=0;i<priceList.size();i+=4){
//            ProductPrice productPrice=new ProductPrice();
//            productPrice.setGrade(priceList.get(i).getGrade());
//            productPrice.setPrice(priceList.get(i).getPriceNum());
//            productPrice.setPrice1(priceList.get(i).getPriceNum());
//            productPrice.setPrice2(priceList.get(i+1).getPriceNum());
//            productPrice.setPrice3(priceList.get(i+2).getPriceNum());
//            productPrice.setPrice4(priceList.get(i+3).getPriceNum());
//            System.out.println(productPrice.toString());
//            priceList1.add(productPrice);
//        }
        for (int i=0;i<priceList.size();i++){
            ProductPrice productPrice=new ProductPrice();
            productPrice.setGrade(priceList.get(i).getGrade());
            productPrice.setPrice(priceList.get(i).getPriceNum());
            productPrice.setPrice1(priceList.get(i).getPriceNum());
            productPrice.setPrice2(priceList.get(i).getPriceNum());
            productPrice.setPrice3(priceList.get(i).getPriceNum());
            productPrice.setPrice4(priceList.get(i).getPriceNum());
            System.out.println(productPrice.toString());
            priceList1.add(productPrice);
        }
        System.out.println(priceList1);
        productVo.setPrice_list(priceList1);
        MallProduct product=mallProductMapper.selectByPrimaryKey(id);
        // Product content required to add
        productVo.setContent(product.getContent()==null? "": product.getContent());

        productVo.setDetail(product.getDetail());
        productVo.setMain_image(product.getMainImage());
        productVo.setStock(product.getStock());
        productVo.setSubtitle(product.getSubtitle());
        productVo.setId(product.getId());
        productVo.setStatus(product.getStatus());
        productVo.setIsnew(product.getIsnew());
        productVo.setTitle(product.getTitle());
        System.out.println("productVo="+productVo.toString());
        return ServerResponse.createBySuccess(productVo);
    }

    public int checkTwisting(String twisting) {
        return mallAttrValueMapper.checkTwisting(twisting);
    }

    public int checkNetwork(String network) {
        return mallAttrValueMapper.checkNetwork(network);
    }
}
